<?php
class WalletaddController extends Ctrl_Base{
    public function indexAction(){
		$tCfos = empty($_GET['cfos'])?0:$_GET['cfos'];

		$this->assign('tCfos' , $tCfos);
		$this->display('walletadd/index');
    }

	private function getwalletall($pKeyword = '',$pCfos=0){
		
		$tWhere = '';
		if(!empty($pKeyword)){
			$tWhere = ' and (name like \'%'.$pKeyword.'%\' or en_name like \'%'.$pKeyword.'%\')';	
		}
		$wData = $this->sqlite()->getAll('select img,beta,name,en_name,mark from wallet where status = 0 and cfos <> 1' . $tWhere . '  group by mark order by sort desc');		

		$tStr = '';
		if($pCfos != 0){$tStr = "style='display: none;'";}

		$tHtml = '';
		$tHtml .= '<div class="tagContent selectTag" id="tagContent0" '.$tStr.'>';
		if(count($wData)){
		foreach($wData as $tRow){
			if($this->l == 'en'){
				$tRow['name'] = ucfirst($tRow['en_name']);
			}
			$tBeta = '';
			if($tRow['beta'] == 1){$tBeta = '<p class="beta"></p>';}
			if(!is_file(WWW_DIR.'/public'.$tRow['img'])){
				$tRow['img'] = 'http://clientapi.taiyi.yuanbao.com/'.$tRow['img'];		
			}
			if(!is_file(WWW_DIR.'/public'.$tRow['small_img'])){
				$tRow['small_img'] = 'http://clientapi.taiyi.yuanbao.com/'.$tRow['small_img'];		
			}
			$tHtml .= '<div class="coin left">
			<a href="/walletadd/introduce?wallet='.$tRow['mark'].'" title="'.$this->lang->walletadd->check_coin_introductions.'">
				<div class="coin_img">
					<p><img src="'.$tRow['img'].'" /></p>
					<h1 class="Font18">'.$tRow['name'].'</h1>
				</div>
			</a>
				<p><a href="/walletadd/introduce?wallet='.$tRow['mark'].'" class="plus_btn">'.$this->lang->walletadd->add.'</a></p>
				'.$tBeta.'
			</div>';
		}
		}else{
			$tHtml .= '<div class="no_con"><p>'.$this->lang->add_a_coin->no_new_coin.'</p></div>';			
		}
		$tHtml .= '</div>';

		$wData = $this->sqlite()->getAll('select beta,img,name,en_name,mark from wallet where status = 0 and cfos = 1' . $tWhere . ' group by mark  order  by sort desc');		
		$tStr = '';
		if($pCfos == 0){$tStr = "style='display: none;'";}
	
		$tHtml .= '<div class="tagContent" id="tagContent1" '.$tStr.'>';
		if(count($wData)){
		foreach($wData as $tRow){
			if($this->l == 'en'){
				$tRow['name'] = ucfirst($tRow['en_name']);
			}
			$tBeta = '';
			if($tRow['beta'] == 1){$tBeta = '<p class="beta"></p>';}
			$tHtml .= '<div class="coin left">
			<a href="/walletadd/introduce?wallet='.$tRow['mark'].'" title="'.$this->lang->walletadd->check_coin_introductions.'">
				<div class="coin_img">
					<p><img src="http://clientapi.taiyi.yuanbao.com/'.$tRow['img'].'" /></p>
					<h1 class="Font18">'.$tRow['name'].'</h1>
				</div>
			</a>
				<p><a href="/walletadd/introduce?wallet='.$tRow['mark'].'" class="plus_btn">'.$this->lang->walletadd->add.'</a></p>
				'.$tBeta.'
			</div>';
		}
		}else{
			$tHtml .= '<div class="no_con"><p>'.$this->lang->add_a_coin->no_new_asset.'</p></div>';			
		}
		$tHtml .= '</div>';
		return $tHtml;
	}

	/**
	 * 同步服务器 钱包列表
	 */
	public function synchronousAction(){
		$this->synchronous();
		exit;
	}
	private function synchronous(){
		header("Content-type: text/html; charset=utf-8");
		$tTime = time();
		$pWalletlist = empty($_GET['walletlist'])?'':trim($_GET['walletlist']);

		$tWallet = Tool_Fnc::sendHttpPostData('http://clientapi.taiyi.yuanbao.com/clientapi/walletlist_cfos',array('walletlist' => $pWalletlist));				
		if(!is_array($tWallet['data'])){Tool_Fnc::ajaxMsg($this->lang->walletadd->synchronizing_server_wallet_list_failed , 0);}

		foreach($tWallet['data'] as $tRow){
			$wRow = Tool_Fnc::sqlite()->getRow('select * from wallet where mark = \''.$tRow['mark'].'\'');						
			if(empty($wRow)){
				Tool_Fnc::writefile(WWW_DIR . '/public/images/'.basename($tRow['small_img']) , file_get_contents($tRow['small_img']));
				Tool_Fnc::writefile(WWW_DIR . '/public/images/'.basename($tRow['img']) , file_get_contents($tRow['img']));
				$tData = array(
					'name' => $tRow['name'],
					'en_name' => $tRow['en_name'],
					'mark' => $tRow['mark'],
					'status' => 0,
					'introduce' => addslashes(htmlspecialchars($tRow['introduce'],ENT_QUOTES)),
					'en_introduce' => addslashes(htmlspecialchars($tRow['en_introduce'],ENT_QUOTES)),
					'rpcuser' => $tRow['rpcuser'],
					'rpcpassword' => $tRow['rpcpassword'],
					'rpcport' => $tRow['rpcport'],
					'created' => $tTime,
					'version' => $tRow['version'],
					'img' => '/images/'.basename($tRow['img']),
					'small_img' =>'/images/'.basename($tRow['small_img']),
					'filesize' => $tRow['filesize'],
					'pos' => $tRow['pos'],
					'block_size' => $tRow['block_size'],
					'explorer' => $tRow['explorer'],
					'cfos' => $tRow['cfos'],
					'beta' => $tRow['beta'],
					'sort' => $tRow['sort'],
				);
				$this->sqlite()->insert('wallet',$tData);		

			}else{

				if(strnatcasecmp($tRow['version'],$wRow['version']) > 0){#版本比较

					Tool_Fnc::writefile(WWW_DIR . '/public/images/'.basename($tRow['small_img']) , file_get_contents($tRow['small_img']));
					Tool_Fnc::writefile(WWW_DIR . '/public/images/'.basename($tRow['img']) , file_get_contents($tRow['img']));
					$tMywalletinfo = $this->sqlite()->getRow('select * from mywallet where mark = \''.$tRow['mark'].'\'');
					$tStatus = 0;
					if(is_array($tMywalletinfo)){
						if(!empty($tMywalletinfo['update'])){continue;}//判断是否已经有升级状态
						$tStatus = 1;	
						$this->sqlite()->query('update mywallet set `update`=1 where mark=\''.$tRow['mark'].'\'');	
						$tData = array(
							'created' => $tTime,
							'wallet' => $pWallet,
							'content' =>   '<a href="/walletedit" class="modify">Please upgrade to a new '.$tRow['en_name'].' version</a>',
							'language' => 'en',
							'status' => 0,
						);
						$this->sqlite()->insert('message',$tData);
						$tData = array(
							'created' => $tTime,
							'wallet' => $pWallet,
							'content' =>   '<a href="/walletedit" class="modify">'.$tRow['name'].' 有新版本更新，点击更新'.$tRow['name'].'。</a>',
							'language' => 'cn',
							'status' => 0,
						);
						$this->sqlite()->insert('message',$tData);
					}

					$this->sqlite()->query('delete from wallet where mark=\''.$tRow['mark'].'\'');
					$tData = array(
					'name' => $tRow['name'],
					'en_name' => $tRow['en_name'],
					'mark' => $tRow['mark'],
					'status' => $tStatus,
					'introduce' => addslashes(htmlspecialchars($tRow['introduce'],ENT_QUOTES)),
					'en_introduce' => addslashes(htmlspecialchars($tRow['en_introduce'],ENT_QUOTES)),

					'rpcuser' => $tRow['rpcuser'],
					'rpcpassword' => $tRow['rpcpassword'],
					'rpcport' => $tRow['rpcport'],
					'created' => $tTime,
					'version' => $tRow['version'],
					'img' => '/images/'.basename($tRow['img']),
					'small_img' =>'/images/'.basename($tRow['small_img']),
					'filesize' => $tRow['filesize'],
					'pos' => $tRow['pos'],
					'block_size' => $tRow['block_size'],
					'explorer' => $tRow['explorer'],
					'cfos' => $tRow['cfos'],
					'beta' => $tRow['beta'],
					'sort' => $tRow['sort'],
					);
					$this->sqlite()->insert('wallet',$tData);	
				}				
			}
		}

	}
	public function searchAction(){
		$pKeyword = empty($_GET['keyword'])?'':trim($_GET['keyword']);
		$pCfos = empty($_GET['cfos'])?0:trim($_GET['cfos']);
		$this->synchronous();
		$tHtml = self::getwalletall($pKeyword,$pCfos);
		Tool_Fnc::ajaxMsg($this->lang->walletadd->synchronized_successfully, 1,$tHtml);
	}

	public function addAction(){
		$pWallet = empty($_GET['wallet'])?'':trim($_GET['wallet']);			
		$pCfos = empty($_GET['cfos'])?'':trim($_GET['cfos']);			
		if($pCfos == 1 && !is_file(SOFT_DIR.'/cfos/wallets/cfos.exe')){
			Tool_Fnc::ajaxMsg('您的太一版本过低,请退出太一重试', 0);
		}

		if(empty($pWallet)){Tool_Fnc::ajaxMsg($this->lang->walletadd->index_error, 0);}
		$tGetwallet = $this->sqlite()->getRow($tSql = 'select * from wallet where mark = \''.$pWallet.'\' and status = 0');
		if(empty($tGetwallet)){Tool_Fnc::ajaxMsg($this->lang->walletadd->there_is_no_information_for_the_coin, 0);}
		
		$tMywallet = $this->getWallet($pWallet);
		if(!empty($tMywallet) && count($tMywallet)){Tool_Fnc::ajaxMsg($this->lang->walletadd->the_coin_purse_has_been_added, 0);}

		$tTime = time();
		$tStatus = 0;
		if($tGetwallet['cfos'] == 1){$tStatus = 3;}
		$tSort = 0;
		#if($tGetwallet['mark'] == 'ybcoin'){$tSort = 1;}

		$tData = array(
			'name' => $tGetwallet['name'],
			'en_name' => $tGetwallet['en_name'],
			'mark' => $tGetwallet['mark'],
			'introduce' => addslashes(htmlspecialchars($tGetwallet['introduce'],ENT_QUOTES)),
			'en_introduce' => addslashes(htmlspecialchars($tGetwallet['en_introduce'],ENT_QUOTES)),
			'rpcuser' => $tGetwallet['rpcuser'],
			'rpcpassword' => $tGetwallet['rpcpassword'],
			'rpcport' => $tGetwallet['rpcport'],
			'created' => $tTime,
			'status' => $tStatus,
			'version' => $tGetwallet['version'],
			'img' => $tGetwallet['img'],
			'small_img' => $tGetwallet['small_img'],
			'filesize' => $tGetwallet['filesize'],
			'pos' => $tGetwallet['pos'],
			'explorer' => $tGetwallet['explorer'],
			'cfos' => $tGetwallet['cfos'],
			'beta' => $tGetwallet['beta'],
			'sort' => $tGetwallet['sort'],
		);
		$this->sqlite()->insert('mywallet',$tData);
		$this->sqlite()->query('update wallet set status = 1,updated='.$tTime.' where mark = \''.$pWallet.'\'');
		Tool_Fnc::ajaxMsg($this->lang->wallet->added_successfully, 1);
		exit;
	}
	public function introduceAction(){
		$pWallet = empty($_GET['wallet'])?'':trim($_GET['wallet']);					
		if(empty($pWallet)){Tool_Fnc::ajaxMsg($this->lang->walletadd->index_error, 0);}
		$tGetwallet = $this->sqlite()->getRow($tSql = 'select * from wallet where mark = \''.$pWallet.'\'');
		if(empty($tGetwallet)){Tool_Fnc::ajaxMsg($this->lang->walletadd->there_is_no_information_for_the_coin, 0);}

		$tWalletinfo = $this->sqlite()->getRow($tSql = 'select * from mywallet where mark = \''.$pWallet.'\'');
		if(!is_file(WWW_DIR.'/public'.$tGetwallet['img'])){
			$tGetwallet['img'] = 'http://clientapi.taiyi.yuanbao.com/'.$tGetwallet['img'];		
		}
		if(!is_file(WWW_DIR.'/public'.$tRow['small_img'])){
			$tGetwallet['small_img'] = 'http://clientapi.taiyi.yuanbao.com/'.$tGetwallet['small_img'];		
		}

		$this->assign('tGetwallet' ,  $tGetwallet);
		$this->assign('tWalletinfo' ,  $tWalletinfo);
		$this->display('walletadd/introduce');
	}
}
